package com.emm.yixun.website.controller.repairData;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.emm.yixun.common.excel.FastExcel;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.HaveReceiveRefund;
import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectBuilding;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.ProjectUnit;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.Subscribe;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.CustomerDto;
import com.emm.yixun.common.model.dto.HaveReceiveRefundDto;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.dto.ProjectBuildingDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.ProjectPmoDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectPremisesPmoDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.dto.ProjectUnitDto;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.enums.BusinessStatusEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.DocTypeEnum;
import com.emm.yixun.common.model.enums.ReceivablesTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.customer.service.SubscribeServiceFacade;
import com.emm.yixun.estate.service.ProjectBuildingServiceFacade;
import com.emm.yixun.estate.service.ProjectPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesServiceFacade;
import com.emm.yixun.estate.service.ProjectRoomServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.estate.service.ProjectUnitServiceFacade;
import com.emm.yixun.marketing.service.HaveReceiveRefundServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.vo.repairData.RepairSignedModel;
import com.emm.yixun.website.vo.repairData.RepairSubscribeModel;

@Controller
@RequestMapping("/repairData/")
public class RepairDataController {
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private ProjectPremisesServiceFacade projectPremisesServiceFacade;
	@Autowired
	private ProjectBuildingServiceFacade projectBuildingServiceFacade;
	@Autowired
	private ProjectUnitServiceFacade projectUnitServiceFacade;
	@Autowired
	private ProjectRoomServiceFacade projectRoomServiceFacade;
	@Autowired
	private ProjectPmoServiceFacade projectPmoServiceFacade;
	@Autowired
	private ProjectPremisesPmoServiceFacade projectPremisesPmoServiceFacade;
	@Autowired
	private SubscribeServiceFacade subscribeServiceFacade;
	@Autowired
	private HaveReceiveRefundServiceFacade haveReceiveRefundServiceFacade;
	@Autowired
	private NeedReceiveRefundServiceFacade needReceiveRefundServiceFacade;
	@Autowired
	private SignedServiceFacade signedServiceFacade;
	@Autowired
	private CustomerServiceFacade customerServiceFacade;
	
	

	@RequestMapping(value = "pmoProjectCode")
	@ResponseBody
	public String pmoProjectCode(Long projectId, String pmoProjectCode) {
		ProjectPmoDto projectPmoDto=new ProjectPmoDto();
		projectPmoDto.setProjectId(projectId);
		projectPmoDto.setPmoProjectCode(pmoProjectCode);
		projectPmoDto.setDelFlag("0");
		projectPmoDto.setMerchantId(Long.valueOf(1));
		Response<ProjectPmoDto> response=projectPmoServiceFacade.save(projectPmoDto);
		if(response.isSuccess()){
			return "success";
		}else{
			return response.getResultMessage();
		}
		
	}
	
	@RequestMapping(value = "pmoPremisesCode")
	@ResponseBody
	public String pmoPremisesCode(Long premisesId, String pmoPremisesCode) {
		Response<ProjectPremises> response=projectPremisesServiceFacade.findById(premisesId);
		ProjectPremises projectPremises=response.getEntity();
		ProjectPremisesPmoDto projectPremisesPmoDto=new ProjectPremisesPmoDto();
		projectPremisesPmoDto.setProjectId(projectPremises.getProjectId());
		projectPremisesPmoDto.setProjectCode(projectPremises.getProjectCode());
		projectPremisesPmoDto.setPremisesCode(projectPremises.getPremisesCode());
		projectPremisesPmoDto.setPremisesId(premisesId);;
		projectPremisesPmoDto.setPmoPremisesCode(pmoPremisesCode);
		projectPremisesPmoDto.setDelFlag("0");
		Response<ProjectPremisesPmoDto> ppResponse=projectPremisesPmoServiceFacade.save(projectPremisesPmoDto);
		if(ppResponse.isSuccess()){
			return "success";
		}else{
			return response.getResultMessage();
		}
		
	}

	// 基于PMO提供的分期数据修复销售系统房产数据
	@RequestMapping(value = "estate")
	@ResponseBody
	public String estate() {

		User user = ShiroUtil.getCurrentUser();
		/*
		 * 南京东方兰园
		 */
		repairOneType("1期", Long.valueOf(52), user);
		/*
		 * 南京东方玉园
		 */
		repairOneType("1期", Long.valueOf(53), user);
		/*
		 * 徐州东方雅园
		 */
		repairOneType("1期", Long.valueOf(51), user);

		/*
		 * 镇江东方诚园
		 */
		repairOneType("1期", Long.valueOf(46), user);
		addPremises("2期", Long.valueOf(46), user);
		/*
		 * 徐州东方润园
		 */
		repairTwoType(user);

		return "success";
	}

	private void repairTwoType(User user) {
		/*
		 * 徐州东方润园 1.修改现有组团名称为“1期”、“2期” 2."一期车位'下的楼栋除("28#","39#")的组团ID更新为1期ID
		 * 3."一期车位'下的楼栋"28#","39#"的组团ID更新为二期期ID 4.删除“一期车位”组团
		 */
		// 一期
		Long premises_137 = Long.valueOf(137);
		// 一期车位
		Long premises_191 = Long.valueOf(191);
		// 二期
		Long premises_200 = Long.valueOf(205);

		// 修改现有组团名称为“1期”、“2期”
		updatePremisesName("1期", premises_137);
		updatePremisesName("2期", premises_200);

		/*
		 * 2."一期车位'下的楼栋除("28#","39#")的组团ID更新为1期ID
		 * 3."一期车位'下的楼栋"28#","39#"的组团ID更新为二期期ID
		 */
		repairBuilding(premises_191, premises_137, premises_200, user);
		// 4.删除“一期车位”组团
		projectPremisesServiceFacade.deleteByPrimaryKey(Long.valueOf(191));
	}

	private void repairBuilding(Long premises_191, Long premises_137,
			Long premises_200, User user) {
		
		Map<Long, String> premisesNameMap = new HashMap<Long, String>();
		premisesNameMap.put(premises_191, "一期车位");
		
		ProjectBuildingDto projectBuildingDto = new ProjectBuildingDto();
		projectBuildingDto.setMerchantId(user.getMerchantId());
		projectBuildingDto.setPremisesId(premises_191);
		ResponseList<ProjectBuilding> pbList = projectBuildingServiceFacade
				.findByDto(projectBuildingDto);
		if (null != pbList.getEntity()) {
			for (ProjectBuilding pb : pbList.getEntity()) {
				ProjectBuildingDto update = new ProjectBuildingDto();
				update.setId(pb.getId());
				String newPremisesName="";
				if (pb.getBuildingName().equals("28#")
						|| pb.getBuildingName().equals("39#")) {
					update.setPremisesId(premises_200);
					newPremisesName="2期";
				} else {
					update.setPremisesId(premises_137);
					newPremisesName="1期";
				}
				Response<Integer> updateResponse = projectBuildingServiceFacade
						.updateByPrimaryKey(update, true);
				if (!updateResponse.isSuccess())
					throw new RuntimeException(
							updateResponse.getResultMessage());
				
				ProjectUnitDto projectUnitDto = new ProjectUnitDto();
				projectUnitDto.setMerchantId(user.getMerchantId());
				projectUnitDto.setBuildingId(pb.getId());
				ResponseList<ProjectUnit> puList = projectUnitServiceFacade
						.findByDto(projectUnitDto);
				if (null != puList.getEntity()) {
					for (ProjectUnit pu : puList.getEntity()) {
						ProjectUnitDto updateUnit = new ProjectUnitDto();
						updateUnit.setId(pu.getId());
						updateUnit.setPremisesId(update.getPremisesId());
						Response<Integer> rowResponse = projectUnitServiceFacade
								.updateByPrimaryKey(updateUnit, true);
						if (!rowResponse.isSuccess())
							throw new RuntimeException(
									rowResponse.getResultMessage());
					}
				}
				
				// 更新房间信息
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setMerchantId(user.getMerchantId());
				projectRoomDto.setBuildingId(pb.getId());
				ResponseList<ProjectRoom> prList = projectRoomServiceFacade
						.findByDto(projectRoomDto);
				if (null != prList.getEntity()) {
					for (ProjectRoom pr : prList.getEntity()) {
						ProjectRoomDto updateRoom = new ProjectRoomDto();
						updateRoom.setId(pr.getId());
						updateRoom.setPremisesId(update.getPremisesId());
						if (StringUtils.isNotBlank(pr.getCombinedName()))
							updateRoom.setCombinedName(getNewCombinedName(
									pr.getPremisesId(), newPremisesName,
									pr.getCombinedName(), premisesNameMap));
						Response<Integer> rowResponse = projectRoomServiceFacade
								.updateByPrimaryKey(updateRoom, true);
						if (!rowResponse.isSuccess())
							throw new RuntimeException(
									rowResponse.getResultMessage());
					}
				}
				
			}
			

		}
	}

	private void repairOneType(String premisesName, Long projectId, User user) {
		/*
		 * 1.新增“1期”组团 2.将项目下楼栋、单元、房间表中的组团ID更新为1期的ID 3.修改房间表combined_name字段
		 * 4.删除掉历史组团
		 */
		List<ProjectPremises> oldPremisesList = queryOld(projectId, user);
		Map<Long, String> premisesNameMap = new HashMap<Long, String>();
		if (null != oldPremisesList) {
			for (ProjectPremises pp : oldPremisesList) {
				premisesNameMap.put(pp.getId(), pp.getGroupName());
			}
		}

		// 新增“1期”组团
		Long premisesId = addPremises(premisesName, projectId, user);
		// 将项目下楼栋、单元、房间表中的组团ID更新为1期的ID;修改房间表combined_name字段
		update(projectId, premisesId, premisesName, user, premisesNameMap);
		// 删除掉历史组团
		delete(oldPremisesList);
	}

	private List<ProjectPremises> queryOld(Long projectId, User user) {
		ProjectPremisesDto query = new ProjectPremisesDto();
		query.setMerchantId(user.getMerchantId());
		query.setProjectId(projectId);
		ResponseList<ProjectPremises> ppList = projectPremisesServiceFacade
				.findByDto(query);
		List<ProjectPremises> resultList = new ArrayList<ProjectPremises>();
		if (null != ppList.getEntity())
			resultList = ppList.getEntity();
		return resultList;
	}

	private void delete(List<ProjectPremises> oldPremisesList) {
		if (null != oldPremisesList) {
			for (ProjectPremises pp : oldPremisesList) {
				Response<Integer> deleteResponse = projectPremisesServiceFacade
						.deleteByPrimaryKey(pp.getId());
				if (!deleteResponse.isSuccess())
					throw new RuntimeException(
							deleteResponse.getResultMessage());
			}
		}

	}

	private void update(Long projectId, Long premisesId,
			String newPremisesName, User user, Map<Long, String> premisesNameMap) {
		// 更新楼栋信息
		ProjectBuildingDto projectBuildingDto = new ProjectBuildingDto();
		projectBuildingDto.setMerchantId(user.getMerchantId());
		projectBuildingDto.setProjectId(projectId);
		ResponseList<ProjectBuilding> pbList = projectBuildingServiceFacade
				.findByDto(projectBuildingDto);
		if (null != pbList.getEntity()) {
			for (ProjectBuilding pb : pbList.getEntity()) {
				ProjectBuildingDto update = new ProjectBuildingDto();
				update.setId(pb.getId());
				update.setPremisesId(premisesId);
				Response<Integer> updateResponse = projectBuildingServiceFacade
						.updateByPrimaryKey(update, true);
				if (!updateResponse.isSuccess())
					throw new RuntimeException(
							updateResponse.getResultMessage());
			}
		}
		// 更新单元信息
		ProjectUnitDto projectUnitDto = new ProjectUnitDto();
		projectUnitDto.setMerchantId(user.getMerchantId());
		projectUnitDto.setProjectId(projectId);
		ResponseList<ProjectUnit> puList = projectUnitServiceFacade
				.findByDto(projectUnitDto);
		if (null != puList.getEntity()) {
			for (ProjectUnit pu : puList.getEntity()) {
				ProjectUnitDto update = new ProjectUnitDto();
				update.setId(pu.getId());
				update.setPremisesId(premisesId);
				Response<Integer> updateResponse = projectUnitServiceFacade
						.updateByPrimaryKey(update, true);
				if (!updateResponse.isSuccess())
					throw new RuntimeException(
							updateResponse.getResultMessage());
			}
		}

		// 更新房间信息
		ProjectRoomDto projectRoomDto = new ProjectRoomDto();
		projectRoomDto.setMerchantId(user.getMerchantId());
		projectRoomDto.setProjectId(projectId);
		ResponseList<ProjectRoom> prList = projectRoomServiceFacade
				.findByDto(projectRoomDto);
		if (null != prList.getEntity()) {
			for (ProjectRoom pr : prList.getEntity()) {
				ProjectRoomDto update = new ProjectRoomDto();
				update.setId(pr.getId());
				update.setPremisesId(premisesId);
				if (StringUtils.isNotBlank(pr.getCombinedName()))
					update.setCombinedName(getNewCombinedName(
							pr.getPremisesId(), newPremisesName,
							pr.getCombinedName(), premisesNameMap));
				Response<Integer> updateResponse = projectRoomServiceFacade
						.updateByPrimaryKey(update, true);
				if (!updateResponse.isSuccess())
					throw new RuntimeException(
							updateResponse.getResultMessage());
			}
		}
	}

	private String getNewCombinedName(Long premisesId, String newPremisesName,
			String combinedName, Map<Long, String> premisesNameMap) {
		String result = combinedName;
		if (null != premisesNameMap) {
			String premisesName = premisesNameMap.get(premisesId);
			if (StringUtils.isNotBlank(premisesName)) {
				result = result.replaceFirst(premisesName, newPremisesName);
			}
		}
		return result;
	}

	private Long addPremises(String premisesName, Long projectId, User user) {
		Response<Project> response = projectServiceFacade.findById(projectId);
		Project project = response.getEntity();
		ProjectPremisesDto premises = new ProjectPremisesDto();
		premises.setMerchantId(user.getMerchantId());
		premises.setUserCode(user.getUserCode());
		premises.setAccessCode(project.getProjectCode());
		premises.setProjectId(projectId);
		premises.setProjectCode(project.getProjectCode());
		premises.setGroupName(premisesName);
		premises.setDeliveryFlag("0");
		premises.setDelFlag("0");
		premises.setCreateUserId(user.getId());
		premises.setRemark("程序修复");
		Response<ProjectPremisesDto> ppdResonse = projectPremisesServiceFacade
				.save(premises);
		return ppdResonse.getEntity().getId();
	}

	private void updatePremisesName(String premisesName, Long premisesId) {
		ProjectPremisesDto update = new ProjectPremisesDto();
		update.setId(premisesId);
		update.setGroupName(premisesName);
		Response<Integer> updateResponse = projectPremisesServiceFacade
				.updateByPrimaryKey(update, true);
		if (!updateResponse.isSuccess())
			throw new RuntimeException(updateResponse.getResultMessage());

	}
	
	@RequestMapping(value = "repairPage")
	public String repairPage(ModelMap map) {
		User user = ShiroUtil.getCurrentUser();
		ProjectDto projectDto=new ProjectDto();
		projectDto.setMerchantId(user.getMerchantId());
		List<Project> projectList=new ArrayList<Project>();
		ResponseList<Project> projectResponse=projectServiceFacade.findByDto(projectDto);
		if(projectResponse.isSuccess() && null!=projectResponse.getEntity()){
			projectList=projectResponse.getEntity();
		}
		map.put("projectList", projectList);
		return "/repairData/repairPage";
	}
	
	@RequestMapping(value = "repairSubscribe")
	public String repairSubscribe(Long projectId,@RequestParam("multipartFile")MultipartFile multipartFile,String flag,ModelMap map) {
		try {
			//解析Excel数据并转为java实体bean
			FastExcel fastExcel = new FastExcel(multipartFile.getInputStream());
			fastExcel.setStartRow(1);
			List<RepairSubscribeModel> customerList = fastExcel.parse(RepairSubscribeModel.class);
			fastExcel.close();
			
			//验证数据
			ResponseList<RepairSubscribeModel> response=checkRepairSubscribeData(customerList,projectId);
			if(!response.isSuccess()){
				map.put("message", response.getResultMessage());
				return "/repairData/repairInfo";
			}
			//修复数据
			if(flag.equals("1")){
				repairSubscribeData(response.getEntity());
			}
			
			map.put("message", "success:"+response.getEntity().size());
		} catch (Exception e) {
			map.put("message",e);
		}
		return "/repairData/repairInfo";
	}
	
	@RequestMapping(value = "repairSigned")
	public String repairSigned(Long projectId,@RequestParam("multipartFile")MultipartFile multipartFile,String flag,ModelMap map,String checkSignedDateOnly) {
		try {
			//解析Excel数据并转为java实体bean
			FastExcel fastExcel = new FastExcel(multipartFile.getInputStream());
			fastExcel.setStartRow(1);
			List<RepairSignedModel> customerList = fastExcel.parse(RepairSignedModel.class);
			fastExcel.close();
			
			//验证数据
			ResponseList<RepairSignedModel> response=checkRepairSignedData(customerList,projectId,checkSignedDateOnly);
			if(!response.isSuccess()){
				map.put("message", response.getResultMessage());
			}else{
				//修复数据
				if(flag.equals("1")){
					repairSignedData(response.getEntity());
				}
				String infoStr="成功条数:"+response.getEntity().size()+";";
				if(StringUtils.isNotBlank(response.getResultMessage())){
					infoStr=infoStr+"失败信息如下：<br/>"+response.getResultMessage();
				}
				map.put("message", infoStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("message",e);
		}
		return "/repairData/repairInfo";
	}
	
	private void repairSignedData(List<RepairSignedModel> list){
		if(null==list)return ;
		for(RepairSignedModel model : list){
			if(StringUtils.isNotBlank(model.getRepairSignedDate())){
				SignedDto signedDto=new SignedDto();
				signedDto.setId(model.getSignedId());
				signedDto.setDidTime(DateUtils.StringToDate(model.getRepairSignedDate(), DateStyle.YYYY_MM_DD));
				signedDto.setCheckDate(DateUtils.StringToDate(model.getRepairSignedDate(), DateStyle.YYYY_MM_DD));
				signedServiceFacade.updateByPrimaryKey(signedDto, true);
			}
			if(StringUtils.isNotBlank(model.getRepairPaymentDate())){
				HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
				haveReceiveRefundDto.setId(model.getHaveReceiveRefundId());
				haveReceiveRefundDto.setStatementDate(DateUtils.StringToDate(model.getRepairPaymentDate(), DateStyle.YYYY_MM_DD));
				haveReceiveRefundServiceFacade.updateByPrimaryKey(haveReceiveRefundDto, true);
			}
			
		}
	}
	
	private void repairSubscribeData(List<RepairSubscribeModel> list){
		if(null==list)return ;
		for(RepairSubscribeModel model : list){
			if(StringUtils.isNotBlank(model.getRepairSubscribeDate())){
				SubscribeDto update=new SubscribeDto();
				update.setId(model.getSubscribeId());
				update.setSubscribeDate(DateUtils.StringToDate(model.getRepairSubscribeDate(), DateStyle.YYYY_MM_DD));
				subscribeServiceFacade.updateByPrimaryKey(update, true);
			}
			if(StringUtils.isNotBlank(model.getRepairPaymentDate())){
				HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
				haveReceiveRefundDto.setId(model.getHaveReceiveRefundId());
				haveReceiveRefundDto.setStatementDate(DateUtils.StringToDate(model.getRepairPaymentDate(), DateStyle.YYYY_MM_DD));
				haveReceiveRefundServiceFacade.updateByPrimaryKey(haveReceiveRefundDto, true);
			}
			
		}
	}
	
	
	private ResponseList<RepairSubscribeModel> checkRepairSubscribeData(List<RepairSubscribeModel> list,Long projectId){
		ResponseList<RepairSubscribeModel>  returnList=new ResponseList<RepairSubscribeModel>(); 
		StringBuffer sb=new StringBuffer("");
		if(null==list){
			returnList.fail("0000", "未能识别excel中的数据");
			return returnList;
		}
		for(RepairSubscribeModel model : list){
			//查询客户
			CustomerDto customerDto=new CustomerDto();
			customerDto.setProjectId(projectId);
			customerDto.setCustomerName(model.getCustomerName());
			ResponseList<Customer> customerList=customerServiceFacade.findByDto(customerDto);
			if(!customerList.isSuccess()){
				sb.append("查询客户：");
				sb.append(model.getCustomerName());
				sb.append("出错<br/>");
				continue;
			}
			if(null==customerList.getEntity() || customerList.getEntity().size()==0){
				sb.append("客户：");
				sb.append(model.getCustomerName());
				sb.append("不存在<br/>");
				continue;
			}
			if(customerList.getEntity().size()>1){
				if(StringUtils.isBlank(model.getCustomerPhone())){
					sb.append("客户：");
					sb.append(model.getCustomerName());
					sb.append("有多个，请确认。<br/>");
					continue;
				}else{
					customerDto=new CustomerDto();
					customerDto.setProjectId(projectId);
					customerDto.setCustomerPhone(model.getCustomerPhone());
					customerList=customerServiceFacade.findByDto(customerDto);
				}
				
			}
			//查询房间
			ProjectRoomDto projectRoomDto=new ProjectRoomDto();
			projectRoomDto.setProjectId(projectId);
			projectRoomDto.setCombinedName(model.getRoomName());
			ResponseList<ProjectRoom> roomList=projectRoomServiceFacade.findByDto(projectRoomDto);
			if(!roomList.isSuccess()){
				sb.append("查询房间：");
				sb.append(model.getRoomName());
				sb.append("出错<br/>");
				continue;
			}
			if(null==roomList.getEntity() || roomList.getEntity().size()==0){
				sb.append("房间：");
				sb.append(model.getRoomName());
				sb.append("不存在<br/>");
				continue;
			}
			if(roomList.getEntity().size()>1){
				sb.append("房间：");
				sb.append(model.getRoomName());
				sb.append("有多个，请确认。<br/>");
				continue;
			}
			Customer customer=customerList.getEntity().get(0);
			ProjectRoom projectRoom=roomList.getEntity().get(0);
			//定位认购
			SubscribeDto subscribeDto=new SubscribeDto();
			subscribeDto.setProjectId(projectId);
			subscribeDto.setCustomerId(customer.getId());
			subscribeDto.setRoomId(projectRoom.getId());
			subscribeDto.setAuditStatus("3");
			ResponseList<Subscribe> subscribeList=subscribeServiceFacade.findByDto(subscribeDto);
			if(!subscribeList.isSuccess()){
				sb.append("查询认购：");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("出错<br/>");
				continue;
			}
			if(null==subscribeList.getEntity() || roomList.getEntity().size()==0){
				sb.append("认购：");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("不存在<br/>");
				continue;
			}
			if(subscribeList.getEntity().size()>1){
				sb.append("");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("有多笔认购，请确认。<br/>");
				continue;
			}
			
			Subscribe subscribe=subscribeList.getEntity().get(0);
			model.setSubscribeId(subscribe.getId());
			
			if(StringUtils.isBlank(model.getPaymentAmount()))continue;
			//定位已收款
			HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
			//已收款
			haveReceiveRefundDto.setDocType("0");
			haveReceiveRefundDto.setCustomerId(customer.getId());
			haveReceiveRefundDto.setRoomId(projectRoom.getId());
			//认购
			haveReceiveRefundDto.setReceivablesType("1");
			haveReceiveRefundDto.setRelationId(subscribe.getId());
			haveReceiveRefundDto.setRealAmount(new BigDecimal(model.getPaymentAmount()));
			ResponseList<HaveReceiveRefund> haveReceiveRefundList=haveReceiveRefundServiceFacade.findByDto(haveReceiveRefundDto);
			if(!haveReceiveRefundList.isSuccess()){
				sb.append("查询已收款：");
				sb.append(subscribe.getId());
				sb.append("出错<br/>");
				continue;
			}
			if(null==haveReceiveRefundList.getEntity() || roomList.getEntity().size()==0){
				sb.append("已收款：");
				sb.append(subscribe.getId());
				sb.append("不存在<br/>");
				continue;
			}
			if(haveReceiveRefundList.getEntity().size()>1){
				sb.append("已收款：");
				sb.append(subscribe.getId());
				sb.append("有多个，请确认。<br/>");
				continue;
			}
			HaveReceiveRefund haveReceiveRefund=haveReceiveRefundList.getEntity().get(0);
			model.setHaveReceiveRefundId(haveReceiveRefund.getId());
		}
		if(sb.toString().equals("")){
			returnList.success(list);
		}else{
			returnList.fail("0000", sb.toString());
		}
		return returnList;
	}
	
	private ResponseList<RepairSignedModel> checkRepairSignedData(List<RepairSignedModel> list,Long projectId, String checkSignedDateOnly){
		ResponseList<RepairSignedModel>  returnList=new ResponseList<RepairSignedModel>(); 
		StringBuffer sb=new StringBuffer("");
		if(null==list){
			returnList.fail("0000", "未能识别excel中的数据");
			return returnList;
		}
		int row=1;
		List<RepairSignedModel> resultList=new ArrayList<RepairSignedModel>();
		for(RepairSignedModel model : list){
			//查询客户
			row++;
			if(StringUtils.isNotBlank(model.getRepairSignedDate())){
				System.out.println("第"+row+"行");
				Date temp=DateUtils.StringToDate(model.getRepairSignedDate(), DateStyle.YYYY_MM_DD);
                if(null==temp){
                	sb.append("第"+row+"行");
    				sb.append("实际签约日期：");
    				sb.append(model.getRepairSignedDate());
    				sb.append("格式错误<br/>");
    				continue;
				}
				
			}
			if(StringUtils.isNotBlank(model.getRepairPaymentDate())){
				Date temp=DateUtils.StringToDate(model.getRepairPaymentDate(), DateStyle.YYYY_MM_DD);
				if(null==temp){
					sb.append("第"+row+"行");
					sb.append("实际回款日期：");
					sb.append(model.getRepairPaymentDate());
					sb.append("格式错误<br/>");
					continue;
				}
				
			}
			//查询房间
			ProjectRoomDto projectRoomDto=new ProjectRoomDto();
			projectRoomDto.setProjectId(projectId);
			projectRoomDto.setPremisesName(model.getPremisesName());
			projectRoomDto.setBuildingName(model.getBuildingName());
			projectRoomDto.setUnitName(model.getUnitName());
			projectRoomDto.setRoomName(model.getRoomName());
			//projectRoomDto.setCombinedName(model.getRoomName());
			ResponseList<ProjectRoom> roomList=projectRoomServiceFacade.findRepairDataRoomByDto(projectRoomDto);
			if(!roomList.isSuccess()){
				sb.append("第"+row+"行");
				sb.append("查询房间：");
				sb.append(model.getRoomName());
				sb.append("出错<br/>");
				continue;
			}
			if(null==roomList.getEntity() || roomList.getEntity().size()==0){
				sb.append("第"+row+"行");
				sb.append("房间：");
				sb.append(model.getRoomName());
				sb.append("不存在<br/>");
				continue;
			}
			if(roomList.getEntity().size()>1){
				sb.append("第"+row+"行");
				sb.append("房间：");
				sb.append(model.getRoomName());
				sb.append("有多个，请确认。<br/>");
				continue;
			}
			//Customer customer=customerList.getEntity().get(0);
			ProjectRoom projectRoom=roomList.getEntity().get(0);
			//定位签约
			SignedDto signedDto=new SignedDto();
			signedDto.setProjectId(projectId);
			//signedDto.setCustomerId(customer.getId());
			signedDto.setRoomId(projectRoom.getId());
			signedDto.setAuditStatus("3");
			signedDto.setBusinessStatus(BusinessStatusEnum.SIGNED.getNo());
			ResponseList<Signed> signedList=signedServiceFacade.findByDto(signedDto);
			if(!signedList.isSuccess()){
				sb.append("第"+row+"行");
				sb.append("查询签约：");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("出错<br/>");
				continue;
			}
			if(null==signedList.getEntity() || roomList.getEntity().size()==0){
				sb.append("第"+row+"行");
				sb.append("签约：");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("不存在<br/>");
				continue;
			}
			if(signedList.getEntity().size()>1){
				sb.append("第"+row+"行");
				sb.append("");
				sb.append(model.getCustomerName());
				sb.append("  ");
				sb.append(model.getRoomName());
				sb.append("有多笔签约，请确认。<br/>");
				continue;
			}
			
			Signed signed=signedList.getEntity().get(0);
			model.setSignedId(signed.getId());
			//只检查房源对应的签约日期,并直接给出提示
			if("1".equals(checkSignedDateOnly)){
				sb.append("第"+row+"行");
				sb.append("待修复日期: ");
				sb.append(model.getRepairSignedDate());
				sb.append("实际日期" + DateUtils.DateToString(signed.getCheckDate(), DateStyle.YYYY_MM_DD_HH_MM_SS));
				sb.append("<br/>");
				continue;
			}
			if(StringUtils.isNotBlank(model.getPaymentAmount())){
				//定位已收款
				HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
				//已收款
				haveReceiveRefundDto.setDocType("0");
				haveReceiveRefundDto.setReceivablesType("2");
				haveReceiveRefundDto.setRelationId(signed.getId());
				haveReceiveRefundDto.setRealAmount(new BigDecimal(model.getPaymentAmount()));
				ResponseList<HaveReceiveRefund> haveReceiveRefundList=haveReceiveRefundServiceFacade.findByDto(haveReceiveRefundDto);
				if(!haveReceiveRefundList.isSuccess()){
					sb.append("第"+row+"行");
					sb.append("查询签约ID：");
					sb.append(signed.getId());
					sb.append("金额：");
					sb.append(model.getPaymentAmount());
					sb.append("对应已收款出错<br/>");
					continue;
				}
				if(null==haveReceiveRefundList.getEntity() || roomList.getEntity().size()==0){
					sb.append("第"+row+"行");
					sb.append("签约ID：");
					sb.append(signed.getId());
					sb.append("金额：");
					sb.append(model.getPaymentAmount());
					sb.append("对应已收款不存在");
					sb.append(nrrErrorMsg(signed.getId()));
					sb.append(hrrErrorMsg(signed.getId()));
					sb.append("<br/>");
					continue;
				}
				if(haveReceiveRefundList.getEntity().size()>1){
					sb.append("第"+row+"行");
					sb.append("签约ID：");
					sb.append(signed.getId());
					sb.append("金额：");
					sb.append(model.getPaymentAmount());
					sb.append("对应已收款有多个，请确认。");
					sb.append(nrrErrorMsg(signed.getId()));
					sb.append(hrrErrorMsg(signed.getId()));
					sb.append("<br/>");
					continue;
				}
				HaveReceiveRefund haveReceiveRefund=haveReceiveRefundList.getEntity().get(0);
				model.setHaveReceiveRefundId(haveReceiveRefund.getId());
			}

			resultList.add(model);
			
		}
		if(list.size()>0){
			returnList.setResultMessage(sb.toString());
			returnList.success(resultList);
		}else{
			returnList.fail("0000", sb.toString());
		}
		return returnList;
	}
	
	/**
	 * 代收款记录信息
	 * @param signedId
	 * @return
	 */
	public String nrrErrorMsg(long signedId){
		String errorMsg = "   ***M2待收款记录如下***  ";
		NeedReceiveRefundDto needReceiveRefundDto=new NeedReceiveRefundDto();
		//已收款
		needReceiveRefundDto.setDocType("0");
		needReceiveRefundDto.setBusinessType(BusinessTypeEnum.SIGNED.getNo());
		needReceiveRefundDto.setRelationId(signedId);
		ResponseList<NeedReceiveRefund> nrrListRes = needReceiveRefundServiceFacade.findByDto(needReceiveRefundDto);
		if(nrrListRes.isSuccess()){
			List<NeedReceiveRefund> nrrList = nrrListRes.getEntity();
			if(null != nrrList && nrrList.size() > 0){
				BigDecimal totalRealAmount = new BigDecimal(0);
				String nrrDetailsInfo = "待收款明细: 【";
				for(int ii=0; ii<nrrList.size(); ii++){
					NeedReceiveRefund nrr = nrrList.get(ii);
					totalRealAmount = totalRealAmount.add(nrr.getTotalReceivablesAmount());
					nrrDetailsInfo += nrr.getTotalReceivablesAmount();
					if(ii != (nrrList.size() - 1)){
						nrrDetailsInfo += ",";
					}
				}
				nrrDetailsInfo += "】";
				errorMsg += "实际待收总额: 【" + totalRealAmount + "】," + nrrDetailsInfo;
			}else{
				errorMsg += "实际待收总额: 【0】, 无收款记录!!!";
			}
		}
		return errorMsg;
	}
	
	/**
	 * 已收款记录信息
	 * @param signedId
	 * @return
	 */
	public String hrrErrorMsg(long signedId){
		String errorMsg = "   ***M2实际收款记录如下***  ";
		HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
		//已收款
		haveReceiveRefundDto.setDocType(DocTypeEnum.COLLECTION.getNo());
		haveReceiveRefundDto.setReceivablesType(ReceivablesTypeEnum.SIGNED.getNo());
		haveReceiveRefundDto.setRelationId(signedId);
		ResponseList<HaveReceiveRefund> hrrListRes=haveReceiveRefundServiceFacade.findByDto(haveReceiveRefundDto);
		if(hrrListRes.isSuccess()){
			List<HaveReceiveRefund> hrrList = hrrListRes.getEntity();
			if(null != hrrList && hrrList.size() > 0){
				BigDecimal totalRealAmount = new BigDecimal(0);
				String hrrDetailsInfo = "收款明细: 【";
				for(int ii=0; ii<hrrList.size(); ii++){
					HaveReceiveRefund hrr = hrrList.get(ii);
					totalRealAmount = totalRealAmount.add(hrr.getRealAmount());
					hrrDetailsInfo += hrr.getRealAmount();
					if(ii != (hrrList.size() - 1)){
						hrrDetailsInfo += ",";
					}
				}
				hrrDetailsInfo += "】";
				errorMsg += "实际总收款: 【" + totalRealAmount + "】," + hrrDetailsInfo;
			}else{
				errorMsg += "实际总收款: 【0】,无收款记录!!!";
			}
		}
		return errorMsg;
	}

}
